#!/bin/bash
# 前置准备
chmod 777 /docker/project/zoey/deploy/
cd /docker/project/zoey/deploy/
rm -rf dist
pwd
tar -zxvf dist.tar.gz
# 定义变量
IMAGE_NAME=zoey-pc:1.0.0
CONTAINER_NAME=zoey-pc

# 判断容器是否存在，若存在则停止并移除
if [ $(docker ps -a -q -f name=$CONTAINER_NAME) ]; then
    echo "Stopping and removing $CONTAINER_NAME container..."
    docker stop $CONTAINER_NAME
    docker rm $CONTAINER_NAME
fi

# 判断镜像是否存在，若存在则移除
if [ $(docker images -q $IMAGE_NAME) ]; then
    echo "Removing old $IMAGE_NAME image..."
    docker rmi $IMAGE_NAME
fi

# 构建镜像
echo "Building new $IMAGE_NAME image..."
docker build --no-cache -t $IMAGE_NAME .

# 启动容器
echo "Starting $CONTAINER_NAME container..."
docker run -d --name $CONTAINER_NAME  --restart=always --network zoey-network -p 9999:9999 $IMAGE_NAME

# 判断容器是否成功启动
if [ $(docker ps -q -f name=$CONTAINER_NAME) ]; then
    echo $CONTAINER_NAME "myapp service is running"
    docker logs -f $CONTAINER_NAME
else
    echo $CONTAINER_NAME "myapp service failed to start"
fi